<template>
  <page-header-wrapper :title="false">
    <a-card
      title="分类管理"
    >
      <template slot="extra">
        <a-button icon="plus" type="danger" @click="addClass">添加分类</a-button>
      </template>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="classColumns"
          :data="classData"
        >
          <template slot="status" slot-scope="text">
            <a-tag color="red" v-if="text == 0">隐藏</a-tag>
            <a-tag color="blue" v-if="text == 1">显示</a-tag>
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button size="small" type="primary" @click="editClass(record.id)">编辑</a-button>
<!--            <a-button icon="plus" size="small" type="primary" @click="addSubClass(record.id)">添加子分类</a-button>-->
            <a-button size="small" type="danger" @click="delClass(record.id)">删除</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { STable } from '@/components'
  import ArticleColumns from '@/columns/common.article'
  import { getArticleClass, getArticleClassDel } from '@/api/article'

  export default {
    components: {
      STable
    },
    name: 'ArticleClass',
    data () {
      return {
        classColumns: ArticleColumns.class,
        classData: parameters => {
          return getArticleClass(Object.assign(parameters, this.queryParam)).then(res => {
            return res.result
          })
        },
        queryParam: {}
      }
    },
    methods: {
      addClass () {
        this.$router.push({ name: 'addClass' })
      },
      addSubClass (pid) {
        this.$router.push({ name: 'addClass', params: { pid } })
      },
      editClass (id) {
        this.$router.push({ name: 'editClass', params: { id } })
      },
      delClass (id) {
        const app = this
        app.$confirm({
          title: '确定删除此分类吗？',
          onOk: () => {
            getArticleClassDel(id).then(res => {
              if (res.status === 0) {
                app.$message.success('删除成功')
                this.$refs.table.refresh(true)
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>
</style>
